<?php

class HomeManageController extends Controller
{

    public function filters()
    {
        return array(
            'accessControl', // perform access control for CRUD operations
        );
    }

    public function accessRules()
    {
        return array(

            array('allow', // allow authenticated user to perform 'create' and 'update' actions
                'actions'=>array('addGoods','add','search','getlist','getbyid','delete'),
                'users'=>array('admin'),
            ),
            array('deny',  // deny all users
                'users'=>array('*'),
            ),
        );
    }

	/**
	 * Declares class-based actions.
	 */
	public function actions()
	{
		return array(
		);
	}

    public function actionAdd()
    {
        $query = array(
            'conditions' => array(
                'link'=>array('=='=>$_POST['link']),
                // 'id'=>array('='=>new MongoId($_POST['id'])),
               // 'channel'=>array('=='=>constant("channel_homepage"))
            ),
        );
        $result = HomeInfo::model()->find($query);
        if(!empty($result)&&(string)$result->_id != $_POST['id'])
        {
            header("HTTP/1.0 400 Not Found");
            die();
        }
        $homeinfo = new HomeInfo();
        if(!empty($_POST['id']))$homeinfo = HomeInfo::model()->findByPk(new MongoId($_POST['id']));
        $homeinfo->channel = $_POST["channel"];
        $homeinfo->image = $_POST["image"];
        $homeinfo->desc_en = $_POST["desc_en"];
        $homeinfo->desc_zn = $_POST["desc_zn"];
        $homeinfo->link = $_POST["link"];
        $homeinfo->updatetime = time();
        
        $homeinfo->save(false);
    }

    public function actionAddGoods($id)
    {
        $query = array(
            'conditions' => array(
                'link'=>array('=='=>$id),
                // 'id'=>array('='=>new MongoId($_POST['id'])),
               // 'channel'=>array('=='=>constant("channel_homepage"))
            ),
        );
        $result = HomeInfo::model()->find($query);
        if(!empty($result))
            return;
        $homeinfo = new HomeInfo();
        // $homeinfo = HomeInfo::model()->findByPk(new MongoId($_POST['id']));
        $homeinfo->channel = "GOODS";
        $homeinfo->image = Goods::model()->findByPk(new MongoId($id))->icon;
        // $homeinfo->desc_en = $_POST["desc_en"];
        // $homeinfo->desc_zn = $_POST["desc_zn"];
        $homeinfo->link = $id;
        $homeinfo->updatetime = time();
        
        $homeinfo->save(false);
    }

    /**
     * 获得版本信息
     */
    public function actionGetList($iDisplayStart=0,$iDisplayLength=10)
    {
        $resultColumn = array('channel','image',);
        $query = array(
            'conditions' => array(
               // 'channel'=>array('=='=>constant("channel_homepage"))
            ),
            'select'=>$resultColumn,
            'limit'=>$iDisplayLength,
            'offset'=>$iDisplayStart,
            'sort'=>array('updatetime'=>EMongoCriteria::SORT_DESC),
        );
        $result = HomeInfo::model()->findAll($query);

        $records = array();
        $records["aaData"] = array(); 

        if(!empty($result))
            foreach ($result as $key => $value) {
            $tmp = array();
            $value->_id = (string)$value->_id;
            foreach ($value as $key1=>$value1) {
                if(in_array($key1, $resultColumn))
                {
                    if($key1=='image')
                        array_push($tmp, "<img src='/dubai/{$value1}' class='img-thumnail' style='max-height:100px' alrt=''>");
                    else
                        array_push($tmp, $value1);
                }
            }
            array_push($tmp, '<a href="javascript:;" data-id="'.$value->_id.'" class="btn btn-xs blue btn-editable"><i class="fa fa-pencil"></i> Edit</a> <a href="javascript:;" data-id="'.$value->_id.'" class="btn btn-xs red btn-removable"><i class="fa fa-times"></i> Delete</a>  ');
            array_push($records["aaData"], $tmp);
        }

        $records["iTotalRecords"] = HomeInfo::model()->count();
        $records["iTotalDisplayRecords"] = HomeInfo::model()->count();
  
        echo json_encode($records);
    }

    public function actionSearch($name)
    {
        $resultColumn = array('name_en','_id');
        $query = array(
            'conditions' => array(
                'status' => array('>'=>0),
                'name_en'=>array('=='=>new MongoRegex("/^{$name}/i")),
                'parentId'=>array('=='=>new MongoRegex("/[1-9]/i")),
                // 'code'=>array('>'=>10),
            ),
            'select'=>$resultColumn,
        );

        $result = HomeInfo::model()->findAll($query);
        $records = array();
        if(!empty($result))
        {
            foreach ($result as $key => $value) {
                $value->_id = (string)$value->_id;
                foreach ($value as $key1=>$value1) {
                    if(!in_array($key1, $resultColumn))
                    {
                        unset($value[$key1]);
                    }
                }

                $obj['id'] = $value->_id;
                $obj['text'] = $value->name_en;
                array_push($records, $obj);
            }
        }



        echoStringToMobile(json_encode(array("results"=>$records)));

    }

    public function actionGetById($id)
    {
        $homeinfo = HomeInfo::model()->findByPk(new MongoId($id));
        $homeinfo->_id = (string)$homeinfo->_id;
        if($homeinfo->channel == 'GOODS')
        {
            $homeinfo = $homeinfo->toArray();
            $homeinfo['linkname'] = Goods::model()->findByPk(new MongoId($homeinfo['link']))->name_en;
        }
        echoStringToMobile(json_encode($homeinfo));
    }

    public function actionDelete($id)
    {
        $category = HomeInfo::model()->deleteByPk(new MongoId($id));
    }
}